package com.htc.dlc;

import java.io.File;
import java.util.ArrayList;

import com.htc.constants.Constants;
import com.htc.dlc.R;
import com.htc.menu.MenuController;
import com.htc.menu.MenuController.MenuSelectedListener;
import com.htc.model.dto.WellnessOverviewDTO;
import com.htc.model.utils.FitnessModel;
import com.htc.utils.CommonActions;
import com.htc.webservice.PicassoUtils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class WellnessSchDetailsActivity extends Activity implements
		MenuSelectedListener {
	MenuController menuController;
	View menuView;
	View menuViewSecondary;
	LinearLayout blurimageView;
	ArrayList<WellnessOverviewDTO> arrDatatime;
	CommonActions ca;
	TextView tv_wellness_sch_detail_title, tv_wellness_sch_detail_desc,
			tv_wellness_sch_detail_rowtime;
	ImageView iv_wellness_sch_detail_image;
	Button back_btn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wellness_sch_details);
		initViews();
		this.menuController = new MenuController(this.menuView,
				this.menuViewSecondary, this.blurimageView, this,
				getBaseContext(), WellnessSchDetailsActivity.this);
		Bundle data = getIntent().getExtras();
		String id = data.getString("ID");
		FitnessModel md = FitnessModel.getInstance();
		arrDatatime = md.getwellnessOverviewDayTime(id, this);
		String str2 = "";
		String time = "";
		for (int j = 0; j < arrDatatime.size(); j++) {
			if (str2.length() == 2)
				str2 = str2 + ":00";
			String str1 = ((WellnessOverviewDTO) this.arrDatatime.get(j))
					.getWellnessoverviewtime();
			if ((str1.length() == 8) || (str1.length() == 7))
				str1 = str1.substring(0, 4);

			if (str1.length() == 2)
				str1 = str1 + ":00";

			if (str1.charAt(2) == '.')
				str1 = str1 + "0";
			time = (time
					+ ((WellnessOverviewDTO) arrDatatime.get(j))
							.getWellnessoverviewday() + " " + str1 + ", ");

			if (time.charAt(j) == '.')
				time = time.replace(".", ":");

		}
		String t = "";
		for (int i = time.length() - 1; i > 0; i--) {
			if (time.charAt(i) == ',') {
				t = time.replace(",", "");
				break;
			}

		}
		String title = data.getString("title");

		String path = data.getString("image");
		String currentUrl = Constants.URL_IMAGE_PAHT + path;
		if (path != null) {
			String[] strs = path.split("/");
			File f = new File(Environment.getExternalStorageDirectory() + "/"
					+ Constants.APP_NAME + "/" + strs[0] + "/" + strs[1]);
			if (f.exists()) {
				if (f.length() == 0) {
					f.delete();
					PicassoUtils.getInstance().showImageFromUrl(
							WellnessSchDetailsActivity.this, currentUrl,
							R.drawable.bgday, iv_wellness_sch_detail_image);
				} else {
					Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
					iv_wellness_sch_detail_image.setImageBitmap(bmp);
				}

			} else {
				PicassoUtils.getInstance().showImageFromUrl(
						WellnessSchDetailsActivity.this, currentUrl,
						R.drawable.bgday, iv_wellness_sch_detail_image);
			}
		}

		String description = data.getString("description");
		tv_wellness_sch_detail_title.setText(title);

		tv_wellness_sch_detail_desc.setText(description);
		tv_wellness_sch_detail_rowtime.setText(t);
	}

	private void initViews() {
		this.back_btn = (Button) findViewById(R.id.back_btn);
		this.back_btn.setVisibility(1);
		this.back_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				int id = v.getId();
				switch (id) {
				case R.id.back_btn: {
					finish();
					overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
					// getActivity().setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);

				}
					break;

				default:
					break;
				}
				// TODO Auto-generated method stub

			}
		});
		// init menu
		menuView = findViewById(R.id.menu_container);
		menuViewSecondary = findViewById(R.id.menu_secondary);
		blurimageView = (LinearLayout) findViewById(R.id.lay_blur_image);
		// init layout
		tv_wellness_sch_detail_title = (TextView) findViewById(R.id.tv_wellness_sch_detail_title);
		iv_wellness_sch_detail_image = (ImageView) findViewById(R.id.iv_wellness_sch_detail_image);
		tv_wellness_sch_detail_desc = (TextView) findViewById(R.id.tv_wellness_sch_detail_desc);
		tv_wellness_sch_detail_rowtime = (TextView) findViewById(R.id.tv_wellness_sch_detail_rowtime);
	}

	@Override
	public void onMenuSelected(int paramInt) {
		// TODO Auto-generated method stub

	}
}
